package com.mxt.moviedemo.controller;
import com.mxt.moviedemo.entity.*;
import com.mxt.moviedemo.service.MoviesService;
import com.mxt.moviedemo.service.TicketService;
import com.mxt.moviedemo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class AdminController {
    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    MoviesService moviesService;

    // 管理员登录
    @PostMapping("login")
    public Map<String, Object> login(@RequestParam("adminUsername") String uname,
                                     @RequestParam("adminPassword") String password) {
        logger.info("Received username: {}, password: {}", uname, password);
        Map<String, Object> result = new HashMap<>();
        try {
            User admin = userService.loginByUsernameAndPassword(uname, password, "管理员");
            logger.info("Login result: {}", admin);
            if (admin != null) {

                result.put("success", true);
                result.put("message", "登录成功");
                result.put("redirectUrl", "/adminSuccessPage");
                result.put("username", admin.getUname());
            } else {
                result.put("success", false);
                result.put("message", "用户名或密码错误");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "登录失败：" + e.getMessage());
        }
        return result;
    }

    // 新增电影
    @PostMapping("addMovie")
    public Map<String, Object> addMovie(@RequestBody Movies movie) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = moviesService.addMovie(movie);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "电影添加成功");
            } else {
                result.put("success", false);
                result.put("message", "电影添加失败");
            }
        } catch (Exception e) {
            logger.error("添加电影错误: ", e);
            result.put("success", false);
            result.put("message", "电影添加失败：" + e.getMessage());
        }
        return result;
    }

    // 删除电影
    @DeleteMapping("deleteMovie/{mid}")
    public Map<String, Object> deleteMovie(@PathVariable int mid) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = moviesService.deleteMovie(mid);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "电影删除成功");
            } else {
                result.put("success", false);
                result.put("message", "电影删除失败");
            }
        } catch (Exception e) {
            logger.error("删除电影错误: ", e);
            result.put("success", false);
            result.put("message", "电影删除失败：" + e.getMessage());
        }
        return result;
    }

    // 修改电影信息
    @PutMapping("updateMovie")
    public Map<String, Object> updateMovie(@RequestBody Movies movie) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = moviesService.updateMovie(movie);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "电影信息修改成功");
            } else {
                result.put("success", false);
                result.put("message", "电影信息修改失败");
            }
        } catch (Exception e) {
            logger.error("修改电影信息错误: ", e);
            result.put("success", false);
            result.put("message", "电影信息修改失败：" + e.getMessage());
        }
        return result;
    }
}